package com.eintsoft.demo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class InesUtil {
	
//	<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.imshealth.com/INES/" xmlns:ns2="http://www.w3.org/2005/08/addressing">
//	<env:Header>
//		<ns2:Action>http://www.imshealth.com/INES/INESRecruitment/INESRecruitment</ns2:Action>
//	</env:Header>
//	<env:Body>
//		<ns1:INESRecruitment>
//			<ns1:INESRecruitmentRequest>
//				<ns1:DoctorID>4</ns1:DoctorID>
//				<ns1:OneKeyID>WCNWXX00012345</ns1:OneKeyID>
//				<ns1:STY_ID>1</ns1:STY_ID >
//				<ns1:CountryISO>CN </ns1:CountryISO>
//				<ns1:FirstName>霄旭</ns1:FirstName>
//				<ns1:LastName>王</ns1:LastName>
//				<ns1:Email>344017085@qq.com</ns1:Email>
//				<ns1:PrimaryLanguage>zh-CN</ns1:PrimaryLanguage>
//				<ns1:Sex>1</ns1:Sex>
//				<ns1:PanelRecruitments>
//					<ns1:INESParticipationStatus>Active</ns1:INESParticipationStatus>
//					<ns1:IncentiveType>LOIncentive</ns1:IncentiveType>
//					<ns1:IncentiveCurrency>CNY</ns1:IncentiveCurrency>
//					<ns1:OneKeySpecialtyCode>XXX</ns1:OneKeySpecialtyCode>
//					<ns1:RegionCode>1</ns1:RegionCode>
//					<ns1:ExtendedProperties></ns1:ExtendedProperties>
//				</ns1:PanelRecruitments>
//			</ns1:INESRecruitmentRequest>
//		</ns1:INESRecruitment>
//	</env:Body>
//</env:Envelope>
	
	public static void translate(String word ) throws Exception {
        //地址
        String urlString = "http://ines-uat.imshealth.com/WebService/INESRecruitment.svc";
        URL url = new URL(urlString);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        
        long doctorId = 4;
        String oneKeyId = "WCNWXX00012345";
        String firstName="霄旭";
        String lastName ="王";
        String email = "344017085@qq.com";
        
        //拼接请求体,此处也可以在外面写xml文件然后读取,但是为了方便一个文件搞定,而且参数也比较好传递我们直接String拼接(直接将网页上的复制进来即可)
        StringBuilder soap = new StringBuilder();
        soap.append("<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns1=\"http://www.imshealth.com/INES/\" xmlns:ns2=\"http://www.w3.org/2005/08/addressing\">");
        soap.append("<env:Header>");
        soap.append("<ns2:Action>http://www.imshealth.com/INES/INESRecruitment/INESRecruitment</ns2:Action>");
        soap.append("</env:Header>");
        soap.append("<env:Body>");
        soap.append("<ns1:INESRecruitment>");
        soap.append("<ns1:INESRecruitmentRequest>");
        soap.append("<ns1:DoctorID>"+doctorId+"</ns1:DoctorID>");
        soap.append("<ns1:OneKeyID>"+oneKeyId+"</ns1:OneKeyID>");
        soap.append("<ns1:STY_ID>1</ns1:STY_ID >");
        soap.append("<ns1:CountryISO>CN </ns1:CountryISO>");
        soap.append("<ns1:FirstName>"+firstName+"</ns1:FirstName>");
        soap.append("<ns1:LastName>"+lastName+"</ns1:LastName>");
        soap.append("<ns1:Email>"+email+"</ns1:Email>");
        soap.append("<ns1:PrimaryLanguage>zh-CN</ns1:PrimaryLanguage>");
        soap.append("<ns1:Sex>1</ns1:Sex>");
        soap.append("<ns1:PanelRecruitments>");
        soap.append("<ns1:INESParticipationStatus>Active</ns1:INESParticipationStatus>");
        soap.append("<ns1:IncentiveType>LOIncentive</ns1:IncentiveType>");
        soap.append("<ns1:IncentiveCurrency>CNY</ns1:IncentiveCurrency>");
        soap.append("<ns1:OneKeySpecialtyCode> XXX </ns1:OneKeySpecialtyCode>");
        soap.append("<ns1:RegionCode>1</ns1:RegionCode>");
        soap.append("<ns1:ExtendedProperties>");
        soap.append("</ns1:ExtendedProperties>");
        soap.append("</ns1:PanelRecruitments>");
        soap.append("</ns1:INESRecruitmentRequest>");
        soap.append("</ns1:INESRecruitment>");
        soap.append("</env:Body>");
        soap.append("</env:Envelope>");
        byte[] buf = soap.toString().getBytes();
        //设置一些头参数
        httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("soapActionString", "INESRecruitment");
        httpConn.setRequestMethod("POST");
        //输入参数和输出结果
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        OutputStream out = httpConn.getOutputStream();
        out.write(buf);
        out.close();

        //最后合格解析结果大家就各显神通了,此处打印出解析的过程,最终得到翻译答案
        byte[] datas = readInputStream(httpConn.getInputStream());
        String result = new String(datas);
        System.out.println("result:" + result);
        //System.out.println(result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7));
        //System.out.println(result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7).replaceAll("</{0,1}(string)?>",""));
    }

    /**
     * 从输入流中读取数据
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }
	
	

	//example https://www.cnblogs.com/garfieldcgf/p/5966317.html
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			InesUtil.translate("hello word");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
